﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScoreComparer
{
    class Program
    {
        const string path = @"D:\studies\AI4\repo\players\all_score_csv.csv";
        static void Main(string[] args)
        {
            try
            {
                string[] file_rows = System.IO.File.ReadAllLines(path);
                List<string> new_rows = new List<string>();
                string[] names = file_rows[0].Split(',');

                StringBuilder header = new StringBuilder();
                header.Append(file_rows[0]);
                for (int i = 0; i < names.Length; i++)
                {
                    header.Append(",");
                    for (int j = 0; j < names.Length; j++)
                    {
                        header.Append(names[i] + "_" + (j + 1).ToString());
                        if (j != names.Length - 1)
                            header.Append(",");
                    }
                }
                new_rows.Add(header.ToString());
               

                foreach (string row in file_rows.Skip(1))
                {
                    
                    string[] values = row.Split(',');
                    Dictionary<string, double> namesValuesDic = new Dictionary<string, double>();
                    string places = row;

                    for (int i = 0; i < names.Length; i++)
                        namesValuesDic[names[i]] = double.Parse(values[i]);


                    foreach (string name in names)
                    {
                        places += generateNewRow(name, namesValuesDic);
                    }
                    new_rows.Add(places);
                }
                System.IO.File.WriteAllLines(path,new_rows.ToArray());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        public static string generateNewRow(string current_name, Dictionary<string, double> dic)
        {
            StringBuilder sb = new StringBuilder();
          
            int place = 0;

            foreach (string name in dic.Keys)
            {
                if (current_name == name)
                    continue;
                else
                {
                    if (dic[name] > dic[current_name])
                        place++;
                }
            }

            for (int i = 0; i < dic.Keys.Count; i++)
            {
                
                sb.Append(",");
                if (place == i)
                    sb.Append("1");
                else
                    sb.Append("0");
            }

            return sb.ToString();
        }
    }
}
